package com.yulongtian.algorithms.data_structure.level1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.cn/problems/intersection-of-two-arrays-ii/
 * 可
 *
 * @author yulongTian
 * @create 2023-03-03 15:25
 */
public class Test04 {
    public static void main(String[] args) {

    }

    public static int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        List<Integer> ans = new ArrayList<>();
        int p1 = 0, p2 = 0;
        int n1 = nums1.length;
        int n2 = nums2.length;
        while (p1 < n1 && p2 < n2) {
            if (nums1[p1] == nums2[p2]) {
                ans.add(nums1[p1]);
                p1++;
                p2++;
            } else if (nums1[p1] < nums2[p2]) {
                p1++;
            } else {
                p2++;
            }
        }
        int[] ans_arr = new int[ans.size()];
        for (int i = 0; i < ans.size(); i++) {
            ans_arr[i] = ans.get(i);
        }
        return ans_arr;
    }

}
